<html lang="en-US">

<head>
    
<title>「连载三」Gin搭建Blog API&#39;s （二） - 花落雨忧</title>

<meta property="og:title" content="「连载三」Gin搭建Blog API&#39;s （二） - 花落雨忧">



    



    
    <meta property="description" content="本节正是编写标签的逻辑，我们想一想，一般接口为增删改查是基础的，那么我们定义一下接口吧！
[&amp;hellip;] 开始编写路由文件逻辑，在routers下新建api目录，我们当前是第一个 API 大版本，因此在api下新建v1目录，再新建tag.go文件，写入内容：
[&amp;hellip;] package v1 import ( &amp;#34;github.com/gin-gonic/gin&amp;#34; ) &amp;hellip;">
    <meta property="og:description" content="本节正是编写标签的逻辑，我们想一想，一般接口为增删改查是基础的，那么我们定义一下接口吧！
[&amp;hellip;] 开始编写路由文件逻辑，在routers下新建api目录，我们当前是第一个 API 大版本，因此在api下新建v1目录，再新建tag.go文件，写入内容：
[&amp;hellip;] package v1 import ( &amp;#34;github.com/gin-gonic/gin&amp;#34; ) &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.site/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.site/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.site/js/jquery.min.js"></script>




<link href="https://www.lican.site/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>
        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/go/gin/2018-02-12-api-02/">「连载三」Gin搭建Blog API&#39;s （二）</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2018/02/12</a>
                                            
                                            
                                            
                                            By 
                                            <a class="author" href="javascript:;">煎鱼</a></div>
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <h2 id="涉及知识点">涉及知识点</h2>
<ul>
<li><a href="https://github.com/gin-gonic/gin">Gin</a>：Golang 的一个微框架，性能极佳。</li>
<li><a href="https://github.com/astaxie/beego/tree/master/validation">beego-validation</a>：本节采用的 beego 的表单验证库，<a href="https://beego.me/docs/mvc/controller/validation.md">中文文档</a>。</li>
<li><a href="https://github.com/jinzhu/gorm">gorm</a>，对开发人员友好的 ORM 框架，<a href="http://gorm.io/docs/">英文文档</a></li>
<li><a href="https://github.com/Unknwon/com">com</a>，一个小而美的工具包。</li>
</ul>
<h2 id="本文目标">本文目标</h2>
<ul>
<li>完成博客的标签类接口定义和编写</li>
</ul>
<h2 id="定义接口">定义接口</h2>
<p>本节正是编写标签的逻辑，我们想一想，一般接口为增删改查是基础的，那么我们定义一下接口吧！</p>
<ul>
<li>获取标签列表：GET(&quot;/tags&rdquo;)</li>
<li>新建标签：POST(&quot;/tags&rdquo;)</li>
<li>更新指定标签：PUT(&quot;/tags/:id&rdquo;)</li>
<li>删除指定标签：DELETE(&quot;/tags/:id&rdquo;)</li>
</ul>
<hr>
<h2 id="编写路由空壳">编写路由空壳</h2>
<p>开始编写路由文件逻辑，在<code>routers</code>下新建<code>api</code>目录，我们当前是第一个 API 大版本，因此在<code>api</code>下新建<code>v1</code>目录，再新建<code>tag.go</code>文件，写入内容：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">v1</span>

<span style="color:#f92672">import</span> (
    <span style="color:#e6db74">&#34;github.com/gin-gonic/gin&#34;</span>
)

<span style="color:#75715e">//获取多个文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetTags</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
}

<span style="color:#75715e">//新增文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">AddTag</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
}

<span style="color:#75715e">//修改文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">EditTag</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
}

<span style="color:#75715e">//删除文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">DeleteTag</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
}
</code></pre></div><h2 id="注册路由">注册路由</h2>
<p>我们打开<code>routers</code>下的<code>router.go</code>文件，修改文件内容为：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">routers</span>

<span style="color:#f92672">import</span> (
    <span style="color:#e6db74">&#34;github.com/gin-gonic/gin&#34;</span>

    <span style="color:#e6db74">&#34;gin-blog/routers/api/v1&#34;</span>
    <span style="color:#e6db74">&#34;gin-blog/pkg/setting&#34;</span>
)

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">InitRouter</span>() <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Engine</span> {
    <span style="color:#a6e22e">r</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">New</span>()

    <span style="color:#a6e22e">r</span>.<span style="color:#a6e22e">Use</span>(<span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Logger</span>())

    <span style="color:#a6e22e">r</span>.<span style="color:#a6e22e">Use</span>(<span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Recovery</span>())

    <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">SetMode</span>(<span style="color:#a6e22e">setting</span>.<span style="color:#a6e22e">RunMode</span>)

    <span style="color:#a6e22e">apiv1</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">r</span>.<span style="color:#a6e22e">Group</span>(<span style="color:#e6db74">&#34;/api/v1&#34;</span>)
    {
        <span style="color:#75715e">//获取标签列表
</span><span style="color:#75715e"></span>        <span style="color:#a6e22e">apiv1</span>.<span style="color:#a6e22e">GET</span>(<span style="color:#e6db74">&#34;/tags&#34;</span>, <span style="color:#a6e22e">v1</span>.<span style="color:#a6e22e">GetTags</span>)
        <span style="color:#75715e">//新建标签
</span><span style="color:#75715e"></span>        <span style="color:#a6e22e">apiv1</span>.<span style="color:#a6e22e">POST</span>(<span style="color:#e6db74">&#34;/tags&#34;</span>, <span style="color:#a6e22e">v1</span>.<span style="color:#a6e22e">AddTag</span>)
        <span style="color:#75715e">//更新指定标签
</span><span style="color:#75715e"></span>        <span style="color:#a6e22e">apiv1</span>.<span style="color:#a6e22e">PUT</span>(<span style="color:#e6db74">&#34;/tags/:id&#34;</span>, <span style="color:#a6e22e">v1</span>.<span style="color:#a6e22e">EditTag</span>)
        <span style="color:#75715e">//删除指定标签
</span><span style="color:#75715e"></span>        <span style="color:#a6e22e">apiv1</span>.<span style="color:#a6e22e">DELETE</span>(<span style="color:#e6db74">&#34;/tags/:id&#34;</span>, <span style="color:#a6e22e">v1</span>.<span style="color:#a6e22e">DeleteTag</span>)
    }

    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">r</span>
}
</code></pre></div><p>当前目录结构：</p>
<pre><code>gin-blog/
├── conf
│   └── app.ini
├── main.go
├── middleware
├── models
│   └── models.go
├── pkg
│   ├── e
│   │   ├── code.go
│   │   └── msg.go
│   ├── setting
│   │   └── setting.go
│   └── util
│       └── pagination.go
├── routers
│   ├── api
│   │   └── v1
│   │       └── tag.go
│   └── router.go
├── runtime
</code></pre><h2 id="检验路由是否注册成功">检验路由是否注册成功</h2>
<p>回到命令行，执行<code>go run main.go</code>，检查路由规则是否注册成功。</p>
<pre><code>$ go run main.go
[GIN-debug] [WARNING] Running in &quot;debug&quot; mode. Switch to &quot;release&quot; mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /api/v1/tags              --&gt; gin-blog/routers/api/v1.GetTags (3 handlers)
[GIN-debug] POST   /api/v1/tags              --&gt; gin-blog/routers/api/v1.AddTag (3 handlers)
[GIN-debug] PUT    /api/v1/tags/:id          --&gt; gin-blog/routers/api/v1.EditTag (3 handlers)
[GIN-debug] DELETE /api/v1/tags/:id          --&gt; gin-blog/routers/api/v1.DeleteTag (3 handlers)
</code></pre><p>运行成功，那么我们愉快的<strong>开始编写我们的接口</strong>吧！</p>
<h2 id="下载依赖包">下载依赖包</h2>
<hr>
<p>首先我们要拉取<code>validation</code>的依赖包，在后面的接口里会使用到表单验证</p>
<pre><code>$ go get -u github.com/astaxie/beego/validation
</code></pre><h2 id="编写标签列表的-models-逻辑">编写标签列表的 models 逻辑</h2>
<p>创建<code>models</code>目录下的<code>tag.go</code>，写入文件内容：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">models</span>

<span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Tag</span> <span style="color:#66d9ef">struct</span> {
    <span style="color:#a6e22e">Model</span>

    <span style="color:#a6e22e">Name</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;name&#34;`</span>
    <span style="color:#a6e22e">CreatedBy</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;created_by&#34;`</span>
    <span style="color:#a6e22e">ModifiedBy</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;modified_by&#34;`</span>
    <span style="color:#a6e22e">State</span> <span style="color:#66d9ef">int</span> <span style="color:#e6db74">`json:&#34;state&#34;`</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetTags</span>(<span style="color:#a6e22e">pageNum</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">pageSize</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">maps</span> <span style="color:#66d9ef">interface</span> {}) (<span style="color:#a6e22e">tags</span> []<span style="color:#a6e22e">Tag</span>) {
    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">maps</span>).<span style="color:#a6e22e">Offset</span>(<span style="color:#a6e22e">pageNum</span>).<span style="color:#a6e22e">Limit</span>(<span style="color:#a6e22e">pageSize</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">tags</span>)

    <span style="color:#66d9ef">return</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetTagTotal</span>(<span style="color:#a6e22e">maps</span> <span style="color:#66d9ef">interface</span> {}) (<span style="color:#a6e22e">count</span> <span style="color:#66d9ef">int</span>){
    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Tag</span>{}).<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">maps</span>).<span style="color:#a6e22e">Count</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">count</span>)

    <span style="color:#66d9ef">return</span>
}
</code></pre></div><ol>
<li>
<p>我们创建了一个<code>Tag struct{}</code>，用于<code>Gorm</code>的使用。并给予了附属属性<code>json</code>，这样子在<code>c.JSON</code>的时候就会自动转换格式，非常的便利</p>
</li>
<li>
<p>可能会有的初学者看到<code>return</code>，而后面没有跟着变量，会不理解；其实你可以看到在函数末端，我们已经显示声明了返回值，这个变量在函数体内也可以直接使用，因为他在一开始就被声明了</p>
</li>
<li>
<p>有人会疑惑<code>db</code>是哪里来的；因为在同个<code>models</code>包下，因此<code>db *gorm.DB</code>是可以直接使用的</p>
</li>
</ol>
<h2 id="编写标签列表的路由逻辑">编写标签列表的路由逻辑</h2>
<p>打开<code>routers</code>目录下 v1 版本的<code>tag.go</code>，第一我们先编写<strong>获取标签列表的接口</strong></p>
<p>修改文件内容：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">v1</span>

<span style="color:#f92672">import</span> (
    <span style="color:#e6db74">&#34;net/http&#34;</span>

    <span style="color:#e6db74">&#34;github.com/gin-gonic/gin&#34;</span>
    <span style="color:#75715e">//&#34;github.com/astaxie/beego/validation&#34;
</span><span style="color:#75715e"></span>    <span style="color:#e6db74">&#34;github.com/Unknwon/com&#34;</span>

    <span style="color:#e6db74">&#34;gin-blog/pkg/e&#34;</span>
    <span style="color:#e6db74">&#34;gin-blog/models&#34;</span>
    <span style="color:#e6db74">&#34;gin-blog/pkg/util&#34;</span>
    <span style="color:#e6db74">&#34;gin-blog/pkg/setting&#34;</span>
)

<span style="color:#75715e">//获取多个文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetTags</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
    <span style="color:#a6e22e">name</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;name&#34;</span>)

    <span style="color:#a6e22e">maps</span> <span style="color:#f92672">:=</span> make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{})
    <span style="color:#a6e22e">data</span> <span style="color:#f92672">:=</span> make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{})

    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">name</span> <span style="color:#f92672">!=</span> <span style="color:#e6db74">&#34;&#34;</span> {
        <span style="color:#a6e22e">maps</span>[<span style="color:#e6db74">&#34;name&#34;</span>] = <span style="color:#a6e22e">name</span>
    }

    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">state</span> <span style="color:#66d9ef">int</span> = <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>
    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">arg</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;state&#34;</span>); <span style="color:#a6e22e">arg</span> <span style="color:#f92672">!=</span> <span style="color:#e6db74">&#34;&#34;</span> {
        <span style="color:#a6e22e">state</span> = <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">arg</span>).<span style="color:#a6e22e">MustInt</span>()
        <span style="color:#a6e22e">maps</span>[<span style="color:#e6db74">&#34;state&#34;</span>] = <span style="color:#a6e22e">state</span>
    }

    <span style="color:#a6e22e">code</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>

    <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;lists&#34;</span>] = <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">GetTags</span>(<span style="color:#a6e22e">util</span>.<span style="color:#a6e22e">GetPage</span>(<span style="color:#a6e22e">c</span>), <span style="color:#a6e22e">setting</span>.<span style="color:#a6e22e">PageSize</span>, <span style="color:#a6e22e">maps</span>)
    <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;total&#34;</span>] = <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">GetTagTotal</span>(<span style="color:#a6e22e">maps</span>)

    <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{
        <span style="color:#e6db74">&#34;code&#34;</span> : <span style="color:#a6e22e">code</span>,
        <span style="color:#e6db74">&#34;msg&#34;</span> : <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetMsg</span>(<span style="color:#a6e22e">code</span>),
        <span style="color:#e6db74">&#34;data&#34;</span> : <span style="color:#a6e22e">data</span>,
    })
}

<span style="color:#75715e">//新增文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">AddTag</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
}

<span style="color:#75715e">//修改文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">EditTag</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
}

<span style="color:#75715e">//删除文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">DeleteTag</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
}
</code></pre></div><ol>
<li><code>c.Query</code>可用于获取<code>?name=test&amp;state=1</code>这类 URL 参数，而<code>c.DefaultQuery</code>则支持设置一个默认值</li>
<li><code>code</code>变量使用了<code>e</code>模块的错误编码，这正是先前规划好的错误码，方便排错和识别记录</li>
<li><code>util.GetPage</code>保证了各接口的<code>page</code>处理是一致的</li>
<li><code>c *gin.Context</code>是<code>Gin</code>很重要的组成部分，可以理解为上下文，它允许我们在中间件之间传递变量、管理流、验证请求的 JSON 和呈现 JSON 响应</li>
</ol>
<p>在本机执行<code>curl 127.0.0.1:8000/api/v1/tags</code>，正确的返回值为<code>{&quot;code&quot;:200,&quot;data&quot;:{&quot;lists&quot;:[],&quot;total&quot;:0},&quot;msg&quot;:&quot;ok&quot;}</code>，若存在问题请结合 gin 结果进行拍错。</p>
<p>在获取标签列表接口中，我们可以根据<code>name</code>、<code>state</code>、<code>page</code>来筛选查询条件，分页的步长可通过<code>app.ini</code>进行配置，以<code>lists</code>、<code>total</code>的组合返回达到分页效果。</p>
<h2 id="编写新增标签的-models-逻辑">编写新增标签的 models 逻辑</h2>
<p>接下来我们编写<strong>新增标签</strong>的接口</p>
<p>打开<code>models</code>目录下的<code>tag.go</code>，修改文件（增加 2 个方法）：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">...</span>
<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">ExistTagByName</span>(<span style="color:#a6e22e">name</span> <span style="color:#66d9ef">string</span>) <span style="color:#66d9ef">bool</span> {
    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">tag</span> <span style="color:#a6e22e">Tag</span>
    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;id&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;name = ?&#34;</span>, <span style="color:#a6e22e">name</span>).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">tag</span>)
    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">tag</span>.<span style="color:#a6e22e">ID</span> &gt; <span style="color:#ae81ff">0</span> {
        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
    }

    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">AddTag</span>(<span style="color:#a6e22e">name</span> <span style="color:#66d9ef">string</span>, <span style="color:#a6e22e">state</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">createdBy</span> <span style="color:#66d9ef">string</span>) <span style="color:#66d9ef">bool</span>{
    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Create</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Tag</span> {
        <span style="color:#a6e22e">Name</span> : <span style="color:#a6e22e">name</span>,
        <span style="color:#a6e22e">State</span> : <span style="color:#a6e22e">state</span>,
        <span style="color:#a6e22e">CreatedBy</span> : <span style="color:#a6e22e">createdBy</span>,
    })

    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
}
<span style="color:#f92672">...</span>
</code></pre></div><h2 id="编写新增标签的路由逻辑">编写新增标签的路由逻辑</h2>
<p>打开<code>routers</code>目录下的<code>tag.go</code>，修改文件（变动 AddTag 方法）：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">v1</span>

<span style="color:#f92672">import</span> (
    <span style="color:#e6db74">&#34;log&#34;</span>
    <span style="color:#e6db74">&#34;net/http&#34;</span>

    <span style="color:#e6db74">&#34;github.com/gin-gonic/gin&#34;</span>
    <span style="color:#e6db74">&#34;github.com/astaxie/beego/validation&#34;</span>
    <span style="color:#e6db74">&#34;github.com/Unknwon/com&#34;</span>

    <span style="color:#e6db74">&#34;gin-blog/pkg/e&#34;</span>
    <span style="color:#e6db74">&#34;gin-blog/models&#34;</span>
    <span style="color:#e6db74">&#34;gin-blog/pkg/util&#34;</span>
    <span style="color:#e6db74">&#34;gin-blog/pkg/setting&#34;</span>
)

<span style="color:#f92672">...</span>

<span style="color:#75715e">//新增文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">AddTag</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
    <span style="color:#a6e22e">name</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;name&#34;</span>)
    <span style="color:#a6e22e">state</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">DefaultQuery</span>(<span style="color:#e6db74">&#34;state&#34;</span>, <span style="color:#e6db74">&#34;0&#34;</span>)).<span style="color:#a6e22e">MustInt</span>()
    <span style="color:#a6e22e">createdBy</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;created_by&#34;</span>)

    <span style="color:#a6e22e">valid</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">validation</span>.<span style="color:#a6e22e">Validation</span>{}
    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Required</span>(<span style="color:#a6e22e">name</span>, <span style="color:#e6db74">&#34;name&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;名称不能为空&#34;</span>)
    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">MaxSize</span>(<span style="color:#a6e22e">name</span>, <span style="color:#ae81ff">100</span>, <span style="color:#e6db74">&#34;name&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;名称最长为100字符&#34;</span>)
    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Required</span>(<span style="color:#a6e22e">createdBy</span>, <span style="color:#e6db74">&#34;created_by&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;创建人不能为空&#34;</span>)
    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">MaxSize</span>(<span style="color:#a6e22e">createdBy</span>, <span style="color:#ae81ff">100</span>, <span style="color:#e6db74">&#34;created_by&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;创建人最长为100字符&#34;</span>)
    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Range</span>(<span style="color:#a6e22e">state</span>, <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;state&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;状态只允许0或1&#34;</span>)

    <span style="color:#a6e22e">code</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">INVALID_PARAMS</span>
    <span style="color:#66d9ef">if</span> ! <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">HasErrors</span>() {
        <span style="color:#66d9ef">if</span> ! <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">ExistTagByName</span>(<span style="color:#a6e22e">name</span>) {
            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>
            <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">AddTag</span>(<span style="color:#a6e22e">name</span>, <span style="color:#a6e22e">state</span>, <span style="color:#a6e22e">createdBy</span>)
        } <span style="color:#66d9ef">else</span> {
            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">ERROR_EXIST_TAG</span>
        }
    }

    <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{
        <span style="color:#e6db74">&#34;code&#34;</span> : <span style="color:#a6e22e">code</span>,
        <span style="color:#e6db74">&#34;msg&#34;</span> : <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetMsg</span>(<span style="color:#a6e22e">code</span>),
        <span style="color:#e6db74">&#34;data&#34;</span> : make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">string</span>),
    })
}
<span style="color:#f92672">...</span>
</code></pre></div><p>用<code>Postman</code>用 POST 访问<code>http://127.0.0.1:8000/api/v1/tags?name=1&amp;state=1&amp;created_by=test</code>，查看<code>code</code>是否返回<code>200</code>及<code>blog_tag</code>表中是否有值，有值则正确。</p>
<h2 id="编写-models-callbacks">编写 models callbacks</h2>
<p>但是这个时候大家会发现，我明明新增了标签，但<code>created_on</code>居然没有值，那做修改标签的时候<code>modified_on</code>会不会也存在这个问题？</p>
<p>为了解决这个问题，我们需要打开<code>models</code>目录下的<code>tag.go</code>文件，修改文件内容（修改包引用和增加 2 个方法）：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">models</span>

<span style="color:#f92672">import</span> (
    <span style="color:#e6db74">&#34;time&#34;</span>

    <span style="color:#e6db74">&#34;github.com/jinzhu/gorm&#34;</span>
)

<span style="color:#f92672">...</span>

<span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">tag</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Tag</span>) <span style="color:#a6e22e">BeforeCreate</span>(<span style="color:#a6e22e">scope</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Scope</span>) <span style="color:#66d9ef">error</span> {
    <span style="color:#a6e22e">scope</span>.<span style="color:#a6e22e">SetColumn</span>(<span style="color:#e6db74">&#34;CreatedOn&#34;</span>, <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Now</span>().<span style="color:#a6e22e">Unix</span>())

    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>
}

<span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">tag</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Tag</span>) <span style="color:#a6e22e">BeforeUpdate</span>(<span style="color:#a6e22e">scope</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Scope</span>) <span style="color:#66d9ef">error</span> {
    <span style="color:#a6e22e">scope</span>.<span style="color:#a6e22e">SetColumn</span>(<span style="color:#e6db74">&#34;ModifiedOn&#34;</span>, <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Now</span>().<span style="color:#a6e22e">Unix</span>())

    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>
}
</code></pre></div><p>重启服务，再在用<code>Postman</code>用 POST 访问<code>http://127.0.0.1:8000/api/v1/tags?name=2&amp;state=1&amp;created_by=test</code>，发现<code>created_on</code>已经有值了！</p>
<p><strong>在这几段代码中，涉及到知识点：</strong></p>
<p>这属于<code>gorm</code>的<code>Callbacks</code>，可以将回调方法定义为模型结构的指针，在创建、更新、查询、删除时将被调用，如果任何回调返回错误，gorm 将停止未来操作并回滚所有更改。</p>
<p><code>gorm</code>所支持的回调方法：</p>
<ul>
<li>创建：BeforeSave、BeforeCreate、AfterCreate、AfterSave</li>
<li>更新：BeforeSave、BeforeUpdate、AfterUpdate、AfterSave</li>
<li>删除：BeforeDelete、AfterDelete</li>
<li>查询：AfterFind</li>
</ul>
<hr>
<h2 id="编写其余接口的路由逻辑">编写其余接口的路由逻辑</h2>
<p>接下来，我们一口气把剩余的两个接口（EditTag、DeleteTag）完成吧</p>
<p>打开<code>routers</code>目录下 v1 版本的<code>tag.go</code>文件，修改内容：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">...</span>

<span style="color:#75715e">//修改文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">EditTag</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
    <span style="color:#a6e22e">id</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Param</span>(<span style="color:#e6db74">&#34;id&#34;</span>)).<span style="color:#a6e22e">MustInt</span>()
    <span style="color:#a6e22e">name</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;name&#34;</span>)
    <span style="color:#a6e22e">modifiedBy</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;modified_by&#34;</span>)

    <span style="color:#a6e22e">valid</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">validation</span>.<span style="color:#a6e22e">Validation</span>{}

    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">state</span> <span style="color:#66d9ef">int</span> = <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>
    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">arg</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;state&#34;</span>); <span style="color:#a6e22e">arg</span> <span style="color:#f92672">!=</span> <span style="color:#e6db74">&#34;&#34;</span> {
        <span style="color:#a6e22e">state</span> = <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">arg</span>).<span style="color:#a6e22e">MustInt</span>()
        <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Range</span>(<span style="color:#a6e22e">state</span>, <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;state&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;状态只允许0或1&#34;</span>)
    }

    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Required</span>(<span style="color:#a6e22e">id</span>, <span style="color:#e6db74">&#34;id&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;ID不能为空&#34;</span>)
    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Required</span>(<span style="color:#a6e22e">modifiedBy</span>, <span style="color:#e6db74">&#34;modified_by&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;修改人不能为空&#34;</span>)
    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">MaxSize</span>(<span style="color:#a6e22e">modifiedBy</span>, <span style="color:#ae81ff">100</span>, <span style="color:#e6db74">&#34;modified_by&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;修改人最长为100字符&#34;</span>)
    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">MaxSize</span>(<span style="color:#a6e22e">name</span>, <span style="color:#ae81ff">100</span>, <span style="color:#e6db74">&#34;name&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;名称最长为100字符&#34;</span>)

    <span style="color:#a6e22e">code</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">INVALID_PARAMS</span>
    <span style="color:#66d9ef">if</span> ! <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">HasErrors</span>() {
        <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>
        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">ExistTagByID</span>(<span style="color:#a6e22e">id</span>) {
            <span style="color:#a6e22e">data</span> <span style="color:#f92672">:=</span> make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{})
            <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;modified_by&#34;</span>] = <span style="color:#a6e22e">modifiedBy</span>
            <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">name</span> <span style="color:#f92672">!=</span> <span style="color:#e6db74">&#34;&#34;</span> {
                <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;name&#34;</span>] = <span style="color:#a6e22e">name</span>
            }
            <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">state</span> <span style="color:#f92672">!=</span> <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span> {
                <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;state&#34;</span>] = <span style="color:#a6e22e">state</span>
            }

            <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">EditTag</span>(<span style="color:#a6e22e">id</span>, <span style="color:#a6e22e">data</span>)
        } <span style="color:#66d9ef">else</span> {
            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">ERROR_NOT_EXIST_TAG</span>
        }
    }

    <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{
        <span style="color:#e6db74">&#34;code&#34;</span> : <span style="color:#a6e22e">code</span>,
        <span style="color:#e6db74">&#34;msg&#34;</span> : <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetMsg</span>(<span style="color:#a6e22e">code</span>),
        <span style="color:#e6db74">&#34;data&#34;</span> : make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">string</span>),
    })
}

<span style="color:#75715e">//删除文章标签
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">DeleteTag</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
    <span style="color:#a6e22e">id</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Param</span>(<span style="color:#e6db74">&#34;id&#34;</span>)).<span style="color:#a6e22e">MustInt</span>()

    <span style="color:#a6e22e">valid</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">validation</span>.<span style="color:#a6e22e">Validation</span>{}
    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Min</span>(<span style="color:#a6e22e">id</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;id&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;ID必须大于0&#34;</span>)

    <span style="color:#a6e22e">code</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">INVALID_PARAMS</span>
    <span style="color:#66d9ef">if</span> ! <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">HasErrors</span>() {
        <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>
        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">ExistTagByID</span>(<span style="color:#a6e22e">id</span>) {
            <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">DeleteTag</span>(<span style="color:#a6e22e">id</span>)
        } <span style="color:#66d9ef">else</span> {
            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">ERROR_NOT_EXIST_TAG</span>
        }
    }

    <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{
        <span style="color:#e6db74">&#34;code&#34;</span> : <span style="color:#a6e22e">code</span>,
        <span style="color:#e6db74">&#34;msg&#34;</span> : <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetMsg</span>(<span style="color:#a6e22e">code</span>),
        <span style="color:#e6db74">&#34;data&#34;</span> : make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">string</span>),
    })
}
</code></pre></div><h2 id="编写其余接口的-models-逻辑">编写其余接口的 models 逻辑</h2>
<p>打开<code>models</code>下的<code>tag.go</code>，修改文件内容：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">...</span>

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">ExistTagByID</span>(<span style="color:#a6e22e">id</span> <span style="color:#66d9ef">int</span>) <span style="color:#66d9ef">bool</span> {
    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">tag</span> <span style="color:#a6e22e">Tag</span>
    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;id&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#a6e22e">id</span>).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">tag</span>)
    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">tag</span>.<span style="color:#a6e22e">ID</span> &gt; <span style="color:#ae81ff">0</span> {
        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
    }

    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">DeleteTag</span>(<span style="color:#a6e22e">id</span> <span style="color:#66d9ef">int</span>) <span style="color:#66d9ef">bool</span> {
    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#a6e22e">id</span>).<span style="color:#a6e22e">Delete</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Tag</span>{})

    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">EditTag</span>(<span style="color:#a6e22e">id</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">data</span> <span style="color:#66d9ef">interface</span> {}) <span style="color:#66d9ef">bool</span> {
    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Tag</span>{}).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#a6e22e">id</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#a6e22e">data</span>)

    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
}
<span style="color:#f92672">...</span>
</code></pre></div><h2 id="验证功能">验证功能</h2>
<p>重启服务，用 Postman</p>
<ul>
<li>PUT 访问 http://127.0.0.1:8000/api/v1/tags/1?name=edit1&amp;state=0&amp;modified_by=edit1 ，查看 code 是否返回 200</li>
<li>DELETE 访问 http://127.0.0.1:8000/api/v1/tags/1 ，查看 code 是否返回 200</li>
</ul>
<p>至此，Tag 的 API&rsquo;s 完成，下一节我们将开始 Article 的 API&rsquo;s 编写！</p>
<h2 id="参考">参考</h2>
<h3 id="本系列示例代码">本系列示例代码</h3>
<ul>
<li><a href="https://github.com/EDDYCJY/go-gin-example">go-gin-example</a></li>
</ul>
<h2 id="关于">关于</h2>
<h3 id="修改记录">修改记录</h3>
<ul>
<li>第一版：2018 年 02 月 16 日发布文章</li>
<li>第二版：2019 年 10 月 01 日修改文章</li>
</ul>
<h2 id="heading">？</h2>
<p>如果有任何疑问或错误，欢迎在 <a href="https://github.com/EDDYCJY/blog">issues</a> 进行提问或给予修正意见，如果喜欢或对你有所帮助，欢迎 Star，对作者是一种鼓励和推进。</p>
<h3 id="我的公众号">我的公众号</h3>
<p><img src="https://image.eddycjy.com/8d0b0c3a11e74efd5fdfd7910257e70b.jpg" alt="image"></p>


                                        
                                        
                                        
                                        <div class="rp4wp-related-posts">
                                            <h3>相关文章</h3>
                                            <ul>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-02-11-api-01/">「连载二」Gin搭建Blog API&#39;s （一）</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-02-10-install/">「连载一」Go 介绍与环境安装</a>
                                                    </div>
                                                </li>
                                                
                                            </ul>
                                        </div>
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        <div class="post-tags">
                                            <ul>
                                            
                                            <li>
                                                <a href="/tags/go" title="View all posts tagged match">go</a>
                                            </li>
                                            
                                            <li>
                                                <a href="/tags/gin" title="View all posts tagged match">gin</a>
                                            </li>
                                            
                                            </ul>
                                        </div>
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.site/posts/go/gin/2018-02-11-api-01/"
                                                    rel="prev">「连载二」Gin搭建Blog API&#39;s （一）</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.site/posts/go/gin/2018-02-13-api-03/">「连载四」Gin搭建Blog API&#39;s （三）</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.site/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.vip All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>